
/*
 **  ITSessionDescMgr.m
 **
 **  Copyright (c) 2002, 2003
 **
 **  Author: Fabian, Ujwal S. Sathyam
 **	     Initial code by Kiichi Kusama
 **
 **  Project: iTerm
 **
 **  Description: keeps track of the address book data.
 **
 **  This program is free software; you can redistribute it and/or modify
 **  it under the terms of the GNU General Public License as published by
 **  the Free Software Foundation; either version 2 of the License, or
 **  (at your option) any later version.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License
 **  along with this program; if not, write to the Free Software
 **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#import "ITSessionDescMgr.h"
#import "ITSessionDesc.h"

NSString* kITSessionDescMgrNotification = @"kITSessionDescMgrNotification";

@interface ITSessionDescMgr (Private)
- (void)sendNotification;
- (void)loadSessionsAtPath:(NSString*)path;
@end

@implementation ITSessionDescMgr

+ (id)sharedInstance;
{
    static ITSessionDescMgr* shared = nil;
    
    if (!shared)
        shared = [[ITSessionDescMgr alloc] init];
    
    return shared;
}

- (id)init;
{
    self = [super init];
            
    _sessionDescs = [[NSMutableArray alloc] init];
    [_sessionDescs addObject:[ITSessionDesc defaultDesc]];  // add one item
    
    return self;
}

- (void)dealloc;
{
    [_sessionDescs release];
    [super dealloc];
}

- (NSArray *)sessionDescs;
{
    return _sessionDescs;
}

- (unsigned)numSessionDescs;
{
    return [_sessionDescs count];
}

- (ITSessionDesc *)sessionDescAtIndex:(int)index;
{
    if ((index >= 0) && (index < [_sessionDescs count]))
        return [_sessionDescs objectAtIndex:index];
    
    return [ITSessionDesc defaultDesc];
}

- (void)addSessionDesc:(ITSessionDesc *)desc;
{
    [_sessionDescs addObject:desc];
    
    [self sendNotification];
}

- (void)removeSessionDesc:(ITSessionDesc *)inDesc;
{
    int i, cnt = [_sessionDescs count];
    ITSessionDesc* desc;
    
    for (i=0;i<cnt;i++)
    {
        desc = [_sessionDescs objectAtIndex:i];
        
        if ([desc isEqualTo:inDesc])
        {            
            [self removeSessionDescAtIndex:i];
            break;
        }
    }
}

- (void)replaceSessionAtIndex:(int)index withObject:(ITSessionDesc*)desc;
{
    [_sessionDescs replaceObjectAtIndex:index withObject:desc];
}

- (void)removeSessionDescAtIndex:(int)index;
{
    if ((index >= 0) && (index < [_sessionDescs count]))
    {
        [_sessionDescs removeObjectAtIndex:index];
        
        [self sendNotification];
    }
}

- (NSArray *)sessionDescNames;
{
    NSEnumerator* enumerator = [_sessionDescs objectEnumerator];
    ITSessionDesc* desc;
    NSMutableArray *result = [NSMutableArray array];
    
    while (desc = [enumerator nextObject])
        [result addObject:[desc name]];
    
    return result;
}

- (void)setSessionFilePath:(NSString*)path
{
    [_filePath autorelease];
    _filePath = [[path stringByExpandingTildeInPath] retain];
    
    if (_filePath)
        [self loadSessionsAtPath:_filePath];
}

- (void)saveSessions;
{
    if (_filePath)
    {      
        if (![NSKeyedArchiver archiveRootObject:_sessionDescs toFile:_filePath])
            NSLog(@"Save failed");
    }
}

@end

@implementation ITSessionDescMgr (Private)

- (void)loadSessionsAtPath:(NSString*)path;
{    
    if ([[NSFileManager defaultManager] fileExistsAtPath:path])
    {
        NSArray* array = [NSKeyedUnarchiver unarchiveObjectWithFile:[path stringByExpandingTildeInPath]];
        
        if (array && [array count] && [array isKindOfClass:[NSArray class]])
        {
            [_sessionDescs autorelease];
            _sessionDescs = [[NSMutableArray arrayWithArray:array] retain];
            
            [self sendNotification];
        }
    }
}

- (void)sendNotification;
{
    [[NSNotificationCenter defaultCenter] postNotificationName:kITSessionDescMgrNotification
                                                        object: nil
                                                      userInfo: nil];    
}

@end